/**************************************************************************
 * Copyright (C) 2012 Marcin Kunicki <masel.popowo@gmail.com>
 * $LastChangedRevision: 66 $
 * $LastChangedBy: masel.popowo@gmail.com $
 * $LastChangedDate: 2012-11-20 12:02:20 +0000 (Tue, 20 Nov 2012) $
 * $HeadURL: http://pps-addressbook.googlecode.com/svn/tags/1.1-a1/src/org/ewicom/ppsaddressbook/UnitDetails.java $
 *
 * This file is part of PPS-AddressBook.
 *
 * PPS-AddressBook is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * PPS-AddressBook is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PPS-AddressBook; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Ten plik jest częścią PPS-AddressBook.
 * 
 * PPS-AddressBook jest wolnym oprogramowaniem; możesz go rozprowadzać dalej
 * i/lub modyfikować na warunkach Powszechnej Licencji Publicznej GNU,
 * wydanej przez Fundację Wolnego Oprogramowania - według wersji 2 tej
 * Licencji lub (według twojego wyboru) którejś z późniejszych wersji.
 * 
 * Niniejszy program rozpowszechniany jest z nadzieją, iż będzie on
 * użyteczny - jednak BEZ JAKIEJKOLWIEK GWARANCJI, nawet domyślnej
 * gwarancji PRZYDATNOŚCI HANDLOWEJ albo PRZYDATNOŚCI DO OKREŚLONYCH
 * ZASTOSOWAŃ. W celu uzyskania bliższych informacji sięgnij do
 * Powszechnej Licencji Publicznej GNU.
 * 
 * Z pewnością wraz z niniejszym programem otrzymałeś też egzemplarz
 * Powszechnej Licencji Publicznej GNU (GNU General Public License);
 * jeśli nie - napisz do Free Software Foundation, Inc., 59 Temple
 * Place, Fifth Floor, Boston, MA  02110-1301  USA
 **************************************************************************/
package org.ewicom.ppsaddressbook;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.ewicom.ppsaddressbook.model.Person;
import org.ewicom.ppsaddressbook.model.PersonDataSource;
import org.ewicom.ppsaddressbook.model.Phone;
import org.ewicom.ppsaddressbook.model.PhoneDataSource;
import org.ewicom.ppsaddressbook.model.Unit;
import org.ewicom.ppsaddressbook.model.UnitDataSource;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.util.Linkify;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class UnitDetails extends Activity {

	private static final String TAG = "UnitDetails";

	private UnitDataSource unitDataSource;
	private PersonDataSource personDataSource;
	private PhoneDataSource phoneDataSource;

	private long unitID;
	private Unit unit;

	private TextView nameTextView;
	private TextView phoneTextView;
	private TextView emailTextView;
	
	private Button addressButton;

	private LinearLayout personLinearLayout;
	private LinearLayout phoneLinearLayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.unitdetails);

		final Intent intent = getIntent();
		unitID = intent.getLongExtra("unit_id", -1);

		setAddressDetails();
		setPersonDetails();
		setPhoneDetails();
	}

	private final void setAddressDetails() {
		nameTextView = (TextView) findViewById(R.id.unit_name);
		phoneTextView = (TextView) findViewById(R.id.unit_phone);
		emailTextView = (TextView) findViewById(R.id.unit_email);
		
		addressButton = (Button) findViewById(R.id.unit_address_button);

		unitDataSource = new UnitDataSource(this);
		unitDataSource.open();

		unit = unitDataSource.getUnitById(unitID);

		nameTextView.setText(unit.getName());
		phoneTextView.setText(unit.getPhone());
		emailTextView.setText(unit.getEmail());
		
		addressButton.setText(unit.getStreet() + "\n" + unit.getCity());

		Linkify.addLinks(phoneTextView, Linkify.PHONE_NUMBERS);
		Linkify.addLinks(emailTextView, Linkify.EMAIL_ADDRESSES);

	}

	private final void setPersonDetails() {
		personLinearLayout = (LinearLayout) findViewById(R.id.unit_details_person);

		personDataSource = new PersonDataSource(this);
		personDataSource.open();

		List<Person> persons = new ArrayList<Person>();
		persons = personDataSource.getPersonsByUnitId(unitID);

		if (persons != null && !persons.isEmpty()) {
			Iterator<Person> iterator = persons.iterator();

			while (iterator.hasNext()) {

				Person person = iterator.next();

				TextView positionTV = (TextView) getLayoutInflater().inflate(
						R.layout.person_position, null);
				TextView personTV = (TextView) getLayoutInflater().inflate(
						R.layout.person_person, null);
				TextView phoneTV = (TextView) getLayoutInflater().inflate(
						R.layout.person_phone, null);
				TextView emailTV = (TextView) getLayoutInflater().inflate(
						R.layout.person_email, null);

				positionTV.setText(person.getPosition());
				personTV.setText(person.getPerson());
				phoneTV.setText(person.getPhone());
				emailTV.setText(person.getEmail());

				Linkify.addLinks(phoneTV, Linkify.PHONE_NUMBERS);
				Linkify.addLinks(emailTV, Linkify.EMAIL_ADDRESSES);

				personLinearLayout.addView(positionTV);
				personLinearLayout.addView(personTV);
				personLinearLayout.addView(phoneTV, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				personLinearLayout.addView(emailTV, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			}
		} else {
			TextView emptyTV = (TextView) getLayoutInflater().inflate(
					R.layout.empty_details, null);
			emptyTV.setText(R.string.emptyPersonDetails);
			personLinearLayout.addView(emptyTV);
		}

	}

	private final void setPhoneDetails() {
		phoneLinearLayout = (LinearLayout) findViewById(R.id.unit_details_phones);

		phoneDataSource = new PhoneDataSource(this);
		phoneDataSource.open();

		List<Phone> phones = new ArrayList<Phone>();
		phones = phoneDataSource.getPhonesByUnitId(unitID);

		if (phones != null && !phones.isEmpty()) {
			Iterator<Phone> iterator = phones.iterator();

			while (iterator.hasNext()) {

				Phone phone = iterator.next();

				TextView phoneTypeTV = (TextView) getLayoutInflater().inflate(
						R.layout.phone_type_tv, null);
				TextView phoneTV = (TextView) getLayoutInflater().inflate(
						R.layout.phone, null);

				phoneTypeTV.setText(phone.getType());
				phoneTV.setText(phone.getPhone());

				Linkify.addLinks(phoneTypeTV, Linkify.PHONE_NUMBERS);
				Linkify.addLinks(phoneTV, Linkify.PHONE_NUMBERS);

				phoneLinearLayout.addView(phoneTypeTV);
				phoneLinearLayout.addView(phoneTV, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			}
		} else {
			TextView emptyTV = (TextView) getLayoutInflater().inflate(
					R.layout.empty_details, null);
			emptyTV.setText(R.string.emptyPhoneDetails);
			phoneLinearLayout.addView(emptyTV);
		}

	}
	
	public void showUnitOnMap(View view){
		String latitude = unit.getLatitude();
		String longitude = unit.getLongitude();
		
		Context context = getApplicationContext();
		Toast toast = Toast.makeText(context, R.string.empty_geodata, Toast.LENGTH_SHORT);
		
		
		if(latitude.equals("BRAK")){
			toast.show();
			Log.d(TAG, "Empty geo data");
		} else {
			String geoUri = "geo:" + latitude + "," + longitude + "?q=" + latitude + "," + longitude + "(" + unit.getShortName() + ")";
			Log.d(TAG, geoUri);
			
			Intent showUnitOnMapIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoUri));
			startActivity(showUnitOnMapIntent);
		}
	}

	@Override
	protected void onResume() {
		unitDataSource.open();
		personDataSource.open();
		phoneDataSource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		unitDataSource.close();
		personDataSource.close();
		phoneDataSource.close();
		super.onPause();
	}
}
